import { selectSql } from '../../util/sqliteHelper'
import _ from 'lodash'

export function getCableWellById(id) {
  return selectSql(`select * from tzinfo where id='${id}'`).then((res) => {
    // 查井的数据
    const wellData = res[0]
    const { pos, detail, pmjson } = wellData
    const parsedDetail = JSON.parse(detail)

    let channels = _.get(parsedDetail, ['剖面信息', '通道电缆段']) || []
    console.log('channels:', channels)
    const allCableSectionIdSet = new Set()
    _.each(channels, (channel) => {
      if (channel['通道下电缆段']) {
        _.each(channel['通道下电缆段'], (cableSection) => {
          allCableSectionIdSet.add(cableSection['电缆段ID'])
        })
      }
    })
    const allCableSectionIds = Array.from(allCableSectionIdSet)

    return selectSql(
      `select * from tzinfo where id in (${allCableSectionIds
        .map((i) => `'${i}'`)
        .join(',')})`
    ).then((res) => {
      const cableSectionMap = _.keyBy(res, 'id') // 关联的电缆段，以id为key
      const allData = {}
      channels.forEach((item) => {
        const id = item['通道ID']
        const cableSections = item['通道下电缆段'].map((i) => {
          const cableSectionId = i['电缆段ID']
          return cableSectionMap[cableSectionId]
        })
        allData[id] = cableSections
      })

      return {
        result: {
          geojson: JSON.parse(pos),
          wellSectionContent: JSON.parse(pmjson),
          allData,
          channels: channels.map((i) => ({
            id: i['通道ID'],
            name: i['通道名称'],
          })),
          cableSections: cableSectionMap,
        },
      }
    })
  })
}

export const wellSectionHoleAPI = {
  enums: {
    status: 'SUCCESS',
    result: {
      holeType: [
        {
          label: '空排管',
          value: 'KongPaiGuan',
        },
        {
          label: '穿孔电缆',
          value: 'ChunKongDianLan',
        },
        {
          label: '直埋电缆',
          value: 'ZhiMaiDianLan',
        },
        {
          label: '穿孔低压',
          value: 'ChuanKongDiYa',
        },
        {
          label: '直埋通讯',
          value: 'ZhiMaiTongXun',
        },
        {
          label: '穿孔通讯',
          value: 'ChuanKongTongXun',
        },
        {
          label: '直埋低压',
          value: 'ZhiMaiDiYa',
        },
        {
          label: '混合管孔',
          value: 'HunHeGuanKong',
        },
      ],
      holeMaterial: [
        {
          label: 'PVC',
          value: 1,
        },
        {
          label: 'PE',
          value: 2,
        },
        {
          label: '波纹管',
          value: 3,
        },
        {
          label: '水泥管',
          value: 4,
        },
        {
          label: '钢管',
          value: 5,
        },
        {
          label: '铸铁',
          value: 6,
        },
        {
          label: '混合',
          value: 7,
        },
        {
          label: '未知',
          value: 8,
        },
        {
          label: '复合',
          value: 9,
        },
        {
          label: '砼',
          value: 10,
        },
      ],
    },
    errorCode: null,
    errorMessage: null,
    extMessage: null,
    timestamp: '1663402603407',
  },
}
